package com.erp.erp_servers.Billing.impl;

import com.erp.erp_dao.Billing.BillingDao;
import com.erp.erp_entitys.Billing.entity.BillingEntity;
import com.erp.erp_entitys.Billing.req.*;
import com.erp.erp_entitys.Billing.vo.*;
import com.erp.erp_entitys.Cost.vo.NameAndCodeVO;
import com.erp.erp_entitys.Cost.vo.QueryImportContractVO;
import com.erp.erp_servers.Billing.BillingService;
import com.erp.utils.DateUtils;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zjk
 * @version 1.0
 * @description
 * @date 2021/1/16/016 23:01
 */
@Service
public class BillingServiceImpl implements BillingService {

    private final BillingDao billingDao;

    public BillingServiceImpl(BillingDao billingDao) {
        this.billingDao = billingDao;
    }

    @Override
    public Integer insertBillingEntity(BillingEntity billingEntity) {
        return billingDao.insertBillingEntity(billingEntity);
    }

    @Override
    public Integer updateBillingEntityByCode(BillingEntity billingEntity) {
        return billingDao.updateBillingEntityByCode(billingEntity);
    }

    @Override
    public BillingEntity selectBillingByCode(String code) {
        return billingDao.selectBillingByCode(code);
    }

    @Override
    public String selectCodeByCode() {
        LocalDate date=LocalDate.now();
        List<String> codeList=billingDao.selectCodeByCode(date);
        String dateStr=date.format(DateTimeFormatter.ofPattern("YYYYMMdd"));
        if (CollectionUtils.isEmpty(codeList)){
            return "KP"+dateStr+"001";
        }
        String code=codeList.get(0);

        String codeStr=code.substring(code.length()-3);
//        Integer codeNum=Integer.parseInt(codeStr);
        //数据转换
        BigDecimal codeNum = new BigDecimal(codeStr);
        //生成新的序列号(序列号自增一位)
        String newSerialNumber = codeNum.add(new BigDecimal("1")).toString();
        if(1 == newSerialNumber.length()){
            newSerialNumber = "00" + newSerialNumber;
        }else if(2 == newSerialNumber.length()){
            newSerialNumber = "0" + newSerialNumber;
        }

        return "KP"+dateStr+newSerialNumber;
    }

    @Override
    public List<QueryBillDeclareVO>  selectBillDeclareByPage(QueryBillDeclareReq req) {
        List<QueryBillDeclareVO> billDeclareVOList=billingDao.selectBillDeclareByPage(req);
        return billDeclareVOList;
    }

    @Override
    public PageInfo<QueryBillDeclareVO2> selectBillDeclare2ByPage(QueryBillDeclareReq req) {
        List<QueryBillDeclareVO2> billDeclareVO2s= billingDao.selectBillDeclare2ByPage(req);
        for (QueryBillDeclareVO2 p:billDeclareVO2s) {
            p.setShippingStr(DateUtils.format(p.getShippingDate(),DateUtils.DATE_PATTERN));
            p.setDeclarationStr(DateUtils.format(p.getDeclarationDate(),DateUtils.DATE_PATTERN));
        }
        PageInfo pageInfo=new PageInfo(billDeclareVO2s);
        return pageInfo;
    }

    @Override
    public List<QueryBillDeclareDetailVO> selectBillDeclareProductByDeclareId(Integer declareId,String manufacturerName) {
        return billingDao.selectBillDeclareProductByDeclareId(declareId,manufacturerName);
    }

    @Override
    public String selectBillDeclareProductCodeByDeclareId(Integer declareId) {
        return billingDao.selectBillDeclareProductCodeByDeclareId(declareId);
    }

    @Override
    public List<QueryPurchaseProductVO> selectPurchaseProductByPurchaseId(Integer purchaseId) {
        List<QueryPurchaseProductVO> purchaseProductVOList = billingDao.selectPurchaseProductByPurchaseId(purchaseId);
        for (QueryPurchaseProductVO p: purchaseProductVOList) {
            //数量
            BigDecimal amount=p.getAmount();
            //价格
            BigDecimal price= p.getPrice();

            p.setSubtotal(price.multiply(amount));

        }
        return purchaseProductVOList;
    }

    @Override
    public List<QueryExportProductVO> selectExportProductByPurchaseId(Integer exportId) {
        List<QueryExportProductVO> purchaseProductVOList = billingDao.selectExportProductByPurchaseId(exportId);
        for (QueryExportProductVO p: purchaseProductVOList) {
            //数量
            BigDecimal amount=p.getAmount();
            //价格
            BigDecimal price= p.getPrice();

            p.setSubtotal(price.multiply(amount));

        }
        return purchaseProductVOList;
    }

    @Override
    public Integer selectInvoicedQuantity(String manufacturerName,String invoiceCode, String stockNo, String productCode, String specCode) {
        return billingDao.selectInvoicedQuantity(manufacturerName,invoiceCode,stockNo,productCode,specCode);
    }

    @Override
    public List<CompanyVO> findAllCustomer2() {
        return billingDao.findAllCustomer2();
    }

    @Override
    public List<CompanyVO> findAllCompany() {
        return billingDao.findAllCompany();
    }

    @Override
    public List<String> getBankName(String code) {
        return billingDao.getBankName(code);
    }

    @Override
    public PageInfo<QueryBillingVO> findBillingList(QueryBillingReq req) {
        List<QueryBillingVO> billingList=billingDao.findBillingList(req);
        for (QueryBillingVO p:billingList){
            p.setBillingDateStr(DateUtils.format(p.getBillingDate(),DateUtils.DATE_PATTERN));
            p.setCreateDateStr(DateUtils.format(p.getCreateDate(),DateUtils.DATE_PATTERN));
        }
        PageInfo pageInfo=new PageInfo(billingList);
        return pageInfo;
    }

    @Override
    public List<QueryBillingVO> findBillingListByMouth(QueryBillingReq req) {
        return billingDao.findBillingList(req);
    }

    @Override
    public Integer updateBillingValidById(Integer[] costIds) {
        return billingDao.updateBillingValidById(costIds);
    }

    @Override
    public R updateBillingStatusById(Integer id, Integer status) {
        int num=billingDao.updateBillingStatusById(id,status);
        if (num>0){
            return R.build(ResultStatusEnum.SUCCESS);
        }
        return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
    }

    @Override
    public BillingEntity selectById(Integer id) {
        return billingDao.selectByid(id);
    }

    @Override
    public List<billingStatistics1VO> billingStatistics1List(billingStatistics1Req req) {
        List<billingStatistics1VO> list=new ArrayList<>();
        if (req.getSourceType()==1){
            list=billingDao.billingStatistics1List(req);
        }else if (req.getSourceType()==2){
            list=billingDao.billingStatistics2List(req);
            List<billingStatistics1VO> list2=billingDao.billingStatistics22List(req);
            list.addAll(list2);

        }
        for (billingStatistics1VO b:list) {
            Integer goodsNum=b.getGoodsNum();
            BigDecimal unitPrice=b.getUnitPrice();
            b.setAmountInvoiced(unitPrice.multiply(BigDecimal.valueOf(goodsNum)));
            b.setNotAmountInvoiced(b.getAmountInvoiced().subtract(b.getSubtotal()));

            b.setBillingDateStr(DateUtils.format(b.getBillingDate(), DateUtils.DATE_PATTERN));
            b.setCreateDateStr(DateUtils.format(b.getCreateDate(), DateUtils.DATE_PATTERN));

            switch (b.getProductType()){
                case "1":
                    b.setProductType("成品");
                    break;
                case "2":
                    b.setProductType("配件");
                    break;
            }
        }
        return list;
    }

    @Override
    public List<BillingPurchaseStatisticsRes> billingPurchaseStatisticsList(BillingPurchaseStatisticsReq req) {
        return billingDao.billingPurchaseStatisticsList(req);
    }

    @Override
    public String getBankNameByCompanyCode(String companyCode) {
        return billingDao.getBankNameByCompanyCode(companyCode);
    }


    @Override
    public List<QueryImportContractVO> getExportContentList(QueryImportExportContractReq req) {
        List<QueryImportContractVO> importContractVOS=billingDao.getExportContentList(req);
        return importContractVOS;
    }

    @Override
    public List<NameAndCodeVO> getPriceTerms() {
        return billingDao.getPriceTerms();
    }

    @Override
    public List<QueryPurchaseContractVO> getPurchaseContractList(QueryPurchaseContractReq req) {
        return billingDao.getPurchaseContractList(req);
    }

    @Override
    public List<QueryImportCarrDetailProductRes> findCarryDetailProductList(QueryImportCarrDetailProductReq req) {
        return billingDao.findCarryDetailProductList(req);
    }

    @Override
    public Integer findCarryDetailProductQuantity(Integer sourceType,String code, String contractCode, String productCode, String specNo) {
        return billingDao.findCarryDetailProductQuantity(sourceType,code,contractCode,productCode,specNo);
    }
}
